package com.imonsoft.sqm.service.basedata;

import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.sqm.dao.basedata.IpicRuleDAO;
import com.imonsoft.sqm.pojo.basedata.IpicRule;
import com.imonsoft.sqm.pojo.basedata.IpicRuleExample;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service("ipicRuleService")
public class IpicRuleServiceImpl implements IpicRuleService {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private IpicRuleDAO ipicRuleDAO;

    @Override
    public List<IpicRule> findIpicRuleByType(String type) {
        if(StringUtils.isEmpty(type)){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        IpicRuleExample example = new IpicRuleExample();
        IpicRuleExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(!StringUtils.isEmpty(type)){
            criteria.andTypeEqualTo(type);
        }
        List<IpicRule> ipicRuleList=ipicRuleDAO.selectIpicRuleByExample(example);
        return ipicRuleList;
    }

    @Override
    public void deleteIpicRule(List<Long> idList) {
        if(null==idList || idList.isEmpty()){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        for(int i=0;i<idList.size();i++){
            IpicRule ipicRule=ipicRuleDAO.selectIpicRuleByPrimaryKey(idList.get(i));
            ipicRule.setState(ConstantSqm.STATUS_DELETE);
            ipicRuleDAO.updateIpicRuleByPrimaryKey(ipicRule);
        }
    }

    @Override
    public Long insert(IpicRule entity) {
        if(null==entity){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        entity.setState(ConstantSqm.STATUS_NORMAL);
        return ipicRuleDAO.insertIpicRule(entity);
    }

    @Override
    public int update(IpicRule entity) {
        if(null==entity || null==entity.getId()){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        IpicRule ipicRule=ipicRuleDAO.selectIpicRuleByPrimaryKey(entity.getId());
        return ipicRuleDAO.updateIpicRuleByPrimaryKey(ipicRule);
    }

    @Override
    public int delete(Long id) {
        if(null==id){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        return ipicRuleDAO.deleteIpicRuleByPrimaryKey(id);
    }

    @Override
    public IpicRule get(Long id) {
        if(null==id){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        IpicRule ipicRule=ipicRuleDAO.selectIpicRuleByPrimaryKey(id);
        return ipicRule;
    }

    @Override
    public List<IpicRule> findList(IpicRule entity) {
        IpicRuleExample example = new IpicRuleExample();
        IpicRuleExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(null!=entity){
            if(!StringUtils.isEmpty(entity.getType())){
                criteria.andTypeLike(entity.getType()+"%");
            }
        }
        List<IpicRule> ipicRuleList=ipicRuleDAO.selectIpicRuleByExample(example);
        return ipicRuleList;
    }

    @Override
    public DataGrid<IpicRule> getDataGrid(IpicRule entity, Pager pager) {
        IpicRuleExample example = new IpicRuleExample();
        example.setOrderByClause("id desc");
        IpicRuleExample.Criteria criteria = example.createCriteria();
        if(null!=entity){
            if(!StringUtils.isEmpty(entity.getType())){
                criteria.andTypeLike(entity.getType() + "%");
            }
        }
        return ipicRuleDAO.getDataGrid(example,pager);
    }
}
